import GameMap from "./GameMap";
import Tower from "./Tower";

const {ccclass, property} = cc._decorator;

@ccclass
export default class TowerSpwan extends cc.Component {
    
    private towerArea:cc.Rect[] = []
    private isTower:boolean[] = []

    @property(cc.SpriteAtlas)
    bottleSpriteAtlas:cc.SpriteAtlas = null

    @property(cc.SpriteAtlas)
    shitSpriteAtlas:cc.SpriteAtlas = null

    private towerNode:cc.Node = null
    private bottleNode:cc.Node = null
    private shitNode:cc.Node = null
    private bottlePre:cc.Node = null

    private isSelect:boolean = false

    onLoad(){
        //开启监听
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
        //获取建塔区域
        this.towerArea = this.getComponent(GameMap).towerArea
    }

    onTouchStart(event:cc.Event.EventTouch){
        //获取触摸点的坐标(世界坐标系)
        const touchPoint = event.getLocation()
        // console.log("touchStart: " + touchPoint)
        let isIntersect = false
        this.towerArea.forEach((item,index) =>{
            //判断触摸点与所有的触摸区域是否相交
            if(item.contains(touchPoint)){
                let pos = this.getComponent(GameMap).tiledMapToCanvas(new cc.Vec2(item.x, item.y))
                isIntersect = true
                if(this.isSelect){
                    this.destoryIcon()
                }
                if(!this.isTower[index]){
                    cc.loader.loadRes("prefabs/bottleNode.prefab", (err, prefab) => {
                        //实例化
                        this.createBottom(prefab, pos)
                        this.createBottleIcon(pos, index)
                        this.createShit(pos)
                        this.isSelect = true
                    })    
                }
            }
        })
        if(isIntersect == false && (this.towerNode && this.bottleNode && this.shitNode)){
            // this.bottlePre.getChildByName("range").active = false
            this.destoryIcon()
        }
    }

    createBottom(prefab:cc.Prefab, pos:cc.Vec2){
        this.towerNode = cc.instantiate(prefab);
        this.towerNode.getChildByName("bottom").active = true
        this.towerNode.getChildByName("atkarea").active = false
        this.towerNode.getChildByName("bottle").active = false
        this.towerNode.setPosition(pos.x + 40, pos.y + 35);
        this.towerNode.setParent(cc.find("Canvas"))
    }

    //创建瓶子炮塔图标
    createBottleIcon(pos:cc.Vec2, index:number){
        //动态创建节点
        this.bottleNode = new cc.Node()
        this.bottleNode.addComponent(cc.Sprite)
        this.bottleNode.addComponent(cc.Button)
        this.bottleNode.getComponent(cc.Sprite).spriteFrame = this.bottleSpriteAtlas.getSpriteFrame("Bottle01")
        this.bottleNode.setPosition(new cc.Vec2(pos.x, pos.y + 35 + 80))
        this.bottleNode.setScale(0)
        const scale = cc.scaleTo(0.2,1)
        this.bottleNode.setParent(cc.find("Canvas/Map"))
        this.bottleNode.runAction(scale)

        //动态添加点击事件
        let clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = cc.find("Canvas/Map"); //这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler.component = "TowerSpwan";//这个是代码文件名
        clickEventHandler.handler = "createBottle";
        clickEventHandler.customEventData = index.toString()
        this.bottleNode.getComponent(cc.Button).clickEvents.push(clickEventHandler)
    }

    createBottle(event, data){
        cc.loader.loadRes("prefabs/bottleNode.prefab", (err, prefab) => {
            this.isTower[parseInt(data)] = true
            //实例化
            this.bottlePre = cc.instantiate(prefab);
            this.bottlePre.getChildByName("atkarea").active = false
            this.bottlePre.getChildByName("bottom").active = false
            this.bottlePre.getChildByName("bottle").active = true
            this.bottlePre.setPosition(this.towerNode.getPosition());
            this.bottlePre.setParent(cc.find("Canvas"))
            // this.bottlePre.addComponent(Tower)

            this.destoryIcon()
        })  
    }

    destoryIcon(){
        this.towerNode.destroy()
        this.bottleNode.destroy()
        this.shitNode.destroy()
    }

    //创建便便炮塔图标
    createShit(pos:cc.Vec2){
        this.shitNode = new cc.Node()
        this.shitNode.addComponent(cc.Sprite)
        this.shitNode.getComponent(cc.Sprite).spriteFrame = this.shitSpriteAtlas.getSpriteFrame("Shit01")
        this.shitNode.setPosition(new cc.Vec2(pos.x + 80, pos.y + 35 + 80))
        this.shitNode.setScale(0)
        const scale1 = cc.scaleTo(0.2,1)
        this.shitNode.setParent(cc.find("Canvas/Map"))
        this.shitNode.runAction(scale1)
    }

    onDisable(){
        //关闭监听
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
    }
}
